package com.bjc.core.controller;

import com.bjc.base.core.annotation.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bjc.base.bean.ResultVO;
import com.bjc.base.core.ResultGenerator;
import com.bjc.base.core.annotation.PermissionPath;
import com.bjc.core.bean.request.UserRoleReqVO;
import com.bjc.core.bean.response.UserRoleVO;
import com.bjc.core.service.IUserRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 用户角色控制器
 * 
 * @author 许自辉
 * @date 2017年8月2日
 */
@RestController
@RequestMapping("api/cloud")
@PermissionPath("系统管理/用户角色")
@Api(value = "userRole-api", tags = { "用户相关接口" })
public class UserRoleController {

	@Autowired
	private IUserRoleService userRoleService;

	@ApiOperation("分页查询用户角色")
	@Permission("userRole:findPage")
	@GetMapping("userRole/findPage")
	public ResultVO getUserRolePage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,
			UserRoleReqVO useroleReqVO) {
		PageHelper.startPage(pageNum, pageSize);
		return ResultGenerator.getSuccess(new PageInfo<UserRoleVO>(userRoleService.getList(useroleReqVO)));
	}

	@ApiOperation("添加用户角色")
	@Permission("userRole:create")
	@PostMapping("userRole/create")
	public ResultVO createUserRole(@RequestBody @Validated UserRoleReqVO useroleReqVO) {
		int count = userRoleService.insert(useroleReqVO);
		return ResultGenerator.getInsertSuccess(count);
	}

	@ApiOperation("更新用户角色")
	@Permission("userRole:update")
	@PutMapping("userRole/update")
	public ResultVO updateUserRole(@RequestBody @Validated UserRoleReqVO useroleReqVO) {
		int count = userRoleService.update(useroleReqVO);
		return ResultGenerator.getUpdateSuccess(count);
	}

	@ApiOperation("删除用户角色")
	@Permission("userRole:delete:id")
	@DeleteMapping("userRole/delete/{id}")
	public ResultVO deleteUserRoleById(@ApiParam(value = "角色id", required = true) @PathVariable("id") Integer id) {
		int count = userRoleService.deleteById(id);
		return ResultGenerator.getDeleteSuccess(count);
	}
}
